/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import logicavista.GestorFichas;
import java.awt.Cursor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

/**
 *
 * @author william
 */
public class Main extends JFrame implements DragGestureListener {

    static boolean dropped;

    public static boolean isDropped() {
        return dropped;
    }

    public static void setDropped(boolean dropped) {
        Main.dropped = dropped;
    }

    @SuppressWarnings("empty-statement")
    public Main() {
        initComponents();

        //Drop fichas en la mesa
        for (int i = 0; i < mesa.a; i++) {
            for (int j = 0; j < mesa.b; j++) {
                new MyDropTargetListener(mesa.casillas[i][j]);
            }
        }
        DragSource ds = new DragSource();

        //Drag fichas del estante
        for (int i = 0; i < estante.a; i++) {
            for (int j = 0; j < estante.b; j++) {
                ds.createDefaultDragGestureRecognizer(estante.casillas[i][j], DnDConstants.ACTION_COPY, this);
            }
        }

        DragSource ds2 = new DragSource();
        //Drag fichas de la mesa
        for (int i = 0; i < mesa.b; i++) {
            for (int j = 0; j < mesa.a; j++) {
                ds2.createDefaultDragGestureRecognizer(mesa.casillas[i][j], DnDConstants.ACTION_COPY, this);
            }
        }
        
        
        
        setLocationRelativeTo(null);
    }

    private void initComponents() {


        mesa = new Mesa(100, 100);
        estante = new Estante(2, 100);
        
        String  [] fichas ={"R12", "N51", "N72", "A32", "R132", "Z122", "N81", "N62", "A42", "R102", "Z112", "N101", "N12", "A72"};
        new GestorFichas().pintarReparticion(estante, fichas);
      
        
        
        scrollPaneMesa = new JScrollPane();
        scrollPaneMesa.setSize(new java.awt.Dimension(700, 520));
        scrollPaneEstante = new JScrollPane();
        scrollPaneEstante.setSize(new java.awt.Dimension(700, 130));
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);


        mesa.setPreferredSize(new java.awt.Dimension(5000, 6500));



        org.jdesktop.layout.GroupLayout tableroGUI1Layout = new org.jdesktop.layout.GroupLayout(mesa);
        mesa.setLayout(tableroGUI1Layout);
        tableroGUI1Layout.setHorizontalGroup(
                tableroGUI1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 5000, Short.MAX_VALUE));//add(0, 0, Short.MAX_VALUE));
        tableroGUI1Layout.setVerticalGroup(
                tableroGUI1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 6500, Short.MAX_VALUE));//add(0, 520, Short.MAX_VALUE));

        scrollPaneMesa.setViewportView(mesa);
        scrollPaneEstante.setViewportView(estante);


        estante.setPreferredSize(new java.awt.Dimension(5000, 130));

        org.jdesktop.layout.GroupLayout estanteLayout = new org.jdesktop.layout.GroupLayout(estante);
        estante.setLayout(estanteLayout);
        estanteLayout.setHorizontalGroup(
                estanteLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 700, Short.MAX_VALUE));
        estanteLayout.setVerticalGroup(
                estanteLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 130, Short.MAX_VALUE));

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(scrollPaneMesa, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 700, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(scrollPaneEstante, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 700, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(0, 118, Short.MAX_VALUE)));




        layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                .add(scrollPaneMesa, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 520, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 23, Short.MAX_VALUE)
                .add(scrollPaneEstante, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 150, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)));


        pack();
    }

    public ArregloFichas getMesa() {
        return mesa;
    }

    public void setMesa(ArregloFichas mesa) {
        this.mesa = mesa;
    }

    public ArregloFichas getEstante() {
        return estante;
    }

    public void setEstante(ArregloFichas estante) {
        this.estante = estante;
    }

    class MyDropTargetListener extends DropTargetAdapter {

        private DropTarget dropTarget;
        private Ficha panel;

        public MyDropTargetListener(Ficha panel) {
            this.panel = panel;

            dropTarget = new DropTarget(panel, DnDConstants.ACTION_COPY,
                    this, true, null);
        }

        public DropTarget getDropTarget() {
            return dropTarget;
        }

        public void setDropTarget(DropTarget dropTarget) {
            this.dropTarget = dropTarget;
        }

        public void drop(DropTargetDropEvent event) {
            try {

                Transferable tr = event.getTransferable();
                ImageIcon image = (ImageIcon) tr.getTransferData(TransferableImage.imageFlavor);

                if (event.isDataFlavorSupported(TransferableImage.imageFlavor)) {

                    event.acceptDrop(DnDConstants.ACTION_COPY);
                    this.panel.setFondo(image);
                    this.panel.repaint();
                    dropped = true;
                    event.dropComplete(dropped);
                    getEstante().pintarVacio();
                    getMesa().pintarVacio();
                    return;
                }
                event.rejectDrop();

            } catch (Exception e) {
                e.printStackTrace();
                event.rejectDrop();
            }
        }
    }

    public static void main(String args[]) {
        
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Main().setVisible(true);
            }
        });
    }
    private ArregloFichas mesa;
    private ArregloFichas estante;
    private JScrollPane scrollPaneMesa;
    private JScrollPane scrollPaneEstante;

    @Override
    public void dragGestureRecognized(DragGestureEvent event) {
        Cursor cursor = null;
        Ficha panel = (Ficha) event.getComponent();

        ImageIcon image = panel.getFondo();

        if (event.getDragAction() == DnDConstants.ACTION_COPY) {
            cursor = DragSource.DefaultCopyDrop;
        }

        event.startDrag(cursor, (Transferable) new TransferableImage(image));
    }
}
